﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;


namespace DAL
{
    class DBConnection
    {
        SqlConnection connect = new SqlConnection();
        public DBConnection()
        {
            connect.ConnectionString = "Data Source=HALTTSE60943;" + "Initial Catalog=QuanLyBanHang;" +
                "User id=sa;" + "Password=123456;"; ;
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
        }
        public DataTable loadData(string sql)
        {
            SqlCommand command = new SqlCommand(sql, connect);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;
        }
        public int Update(string sql, string[] name, object[] value, int nparameter)
        {
            SqlCommand command = new SqlCommand(sql, connect);
            for (int i = 0; i < nparameter; i++)
            {
                command.Parameters.AddWithValue(name[i], value[i]);
            }
            return command.ExecuteNonQuery();
        }
        public DataSet LoadDBComBox(string sql)
        {
            SqlCommand command = new SqlCommand(sql, connect);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            return ds;
        }
        public bool checkExistedItem(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, connect);
            if (cmd.ExecuteScalar() != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public int InsertToDB(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, connect);
            return cmd.ExecuteNonQuery();
        }
    }

}
